/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import Entidades.Persona;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Julian
 */
@Stateless
public class FachadaPersona {

    @PersistenceContext(unitName = "ProyectosPU")
    private EntityManager em;

    public void adicionar(Persona persona) {
        em.persist(persona);
    }

    public void actualizar(Persona persona) {
        em.merge(persona);
    }

    public void eliminar(Persona persona) {
        em.remove(em.merge(persona));
    }

    public ArrayList<Persona> listar() {
        ArrayList<Persona> personas = new ArrayList();
        Query query = em.createQuery("select p from Persona p");
        
        for (Object o : query.getResultList()) {
            personas.add((Persona) o);
        }
        return personas;
    }

    public Persona buscarPorCedula(String cedula) {
        Persona per=null;
        ArrayList<Persona> personas = new ArrayList();
        Query query = em.createQuery("select p from Persona p where p.cedula=?1");
        query.setParameter(1, cedula);

        for (Object o : query.getResultList()) {
            personas.add((Persona) o);
            per = ((Persona) o);
        }
        return per;
    }

}
